package top.drinyeit.weilong.presenter;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;

import java.util.List;
import java.util.Map;

import top.drinyeit.weilong.activity.WithdrawDetailActivity;
import top.drinyeit.weilong.adapter.WithdrawDeatilAdapter;
import top.drinyeit.weilong.model.WithdrawDetailBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/24.
 */

public class WithdrawDetailPresenter {
    private WithdrawDetailActivity activity;
    private WithdrawDeatilAdapter withdrawDeatilAdapter;
    public WithdrawDetailPresenter(WithdrawDetailActivity activity){
        this.activity = activity;
    }

    //获取提现明细
    public void getWithdrawDetail(Map<String,Object> map) {
        final int index = (int) map.get("pageIndex");
        CommonSubscriber<HttpResponse<List<WithdrawDetailBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<WithdrawDetailBean>>>() {
            @Override
            public void onError(String e, int code) {
                activity.shSwipeRefreshLayout.finishRefresh();
                activity.shSwipeRefreshLayout.finishLoadmore();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<List<WithdrawDetailBean>> o) {
                activity.shSwipeRefreshLayout.finishRefresh();
                activity.shSwipeRefreshLayout.finishLoadmore();
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.pageIndex = index;
                    if (index == 1) {
                        if (withdrawDeatilAdapter == null) {
                            if (o.getData() != null && o.getData().size() > 0) {
                                withdrawDeatilAdapter = new WithdrawDeatilAdapter(o.getData());
                                activity.recyclerView.setLayoutManager(new LinearLayoutManager(activity));
                                activity.recyclerView.setAdapter(withdrawDeatilAdapter);
                            }
                        } else {
                            if (o.getData() == null || o.getData().size() == 0) {
                                withdrawDeatilAdapter.clear();
                            } else {
                                withdrawDeatilAdapter.setData(o.getData());
                            }
                            withdrawDeatilAdapter.notifyDataSetChanged();
                        }
                    } else if (index > 1) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            withdrawDeatilAdapter.addData(o.getData());
                            withdrawDeatilAdapter.notifyItemRangeInserted(withdrawDeatilAdapter.getData().size(), o.getData().size());
                        }else {
                            activity.showToastMsg("没有更多啦");
                        }
                    }

                }else{

                }
            }
        });
        RetrofitMethod.getInstance().getWithdrawDetail(subscriber, map);
    }
}
